//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LineSearchBasedMethod.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Optimization/LineSearch.h>
#include <gen/QL/Math/Optimization/Problem.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CLineSearchBasedMethod::CLineSearchBasedMethod (boost::shared_ptr<QuantLib::LineSearchBasedMethod>& childNative, Object^ owner) : COptimizationMethod(CLineSearchBasedMethod::typeid)
{
#ifdef HANDLE
	_phLineSearchBasedMethod = NULL;
#endif
	_ppLineSearchBasedMethod = &childNative;
    _ppOptimizationMethod = new boost::shared_ptr<QuantLib::OptimizationMethod> (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLineSearchBasedMethod));
}
Cephei::QL::Math::Optimization::CLineSearchBasedMethod::CLineSearchBasedMethod (QuantLib::LineSearchBasedMethod& childNative, Object^ owner) : COptimizationMethod(CLineSearchBasedMethod::typeid)
{
#ifdef HANDLE
	_phLineSearchBasedMethod = NULL;
#endif
	_ppLineSearchBasedMethod = new boost::shared_ptr<QuantLib::LineSearchBasedMethod> (&childNative);
    _ppOptimizationMethod = new boost::shared_ptr<QuantLib::OptimizationMethod> (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLineSearchBasedMethod));
    _LineSearchBasedMethodOwner = owner;
    _OptimizationMethodOwner = owner;
}

Cephei::QL::Math::Optimization::CLineSearchBasedMethod::CLineSearchBasedMethod (CLineSearchBasedMethod^ copy) : COptimizationMethod(CLineSearchBasedMethod::typeid)
{
#ifdef HANDLE
	_phLineSearchBasedMethod = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLineSearchBasedMethod = new boost::shared_ptr<QuantLib::LineSearchBasedMethod> (copy->GetShared());
        _ppOptimizationMethod = new boost::shared_ptr<QuantLib::OptimizationMethod> (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLineSearchBasedMethod));
    }
}
Cephei::QL::Math::Optimization::CLineSearchBasedMethod::CLineSearchBasedMethod (System::Type^ t) : COptimizationMethod(CLineSearchBasedMethod::typeid)
{
#ifdef HANDLE
	_phLineSearchBasedMethod = NULL;
#endif
	if (!t->IsSubclassOf(CLineSearchBasedMethod::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CLineSearchBasedMethod::CLineSearchBasedMethod (QuantLib::Handle<QuantLib::LineSearchBasedMethod>& childNative, Object^ owner)  : COptimizationMethod(CLineSearchBasedMethod::typeid)
{
	_phLineSearchBasedMethod = &childNative;
	_ppLineSearchBasedMethod = &static_cast<boost::shared_ptr<QuantLib::LineSearchBasedMethod>>(childNative.currentLink());
    _ppOptimizationMethod = new boost::shared_ptr<QuantLib::OptimizationMethod> (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLineSearchBasedMethod));
    _LineSearchBasedMethodOwner = owner;
}
Cephei::QL::Math::Optimization::CLineSearchBasedMethod::CLineSearchBasedMethod (QuantLib::Handle<QuantLib::LineSearchBasedMethod> childNative)  : COptimizationMethod(CLineSearchBasedMethod::typeid)
{
	_phLineSearchBasedMethod = &childNative;
	_ppLineSearchBasedMethod = &static_cast<boost::shared_ptr<QuantLib::LineSearchBasedMethod>>(childNative.currentLink());
    _ppOptimizationMethod = new boost::shared_ptr<QuantLib::OptimizationMethod> (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLineSearchBasedMethod));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CLineSearchBasedMethod::CLineSearchBasedMethod (QuantLib::LineSearchBasedMethod childNative)  : COptimizationMethod(CLineSearchBasedMethod::typeid)
{
#ifdef HANDLE
	_phLineSearchBasedMethod = NULL;
#endif
	_ppLineSearchBasedMethod = new boost::shared_ptr<QuantLib::LineSearchBasedMethod> (new QuantLib::LineSearchBasedMethod (childNative));
    _ppOptimizationMethod = new boost::shared_ptr<QuantLib::OptimizationMethod> (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLineSearchBasedMethod));
}
#endif

Cephei::QL::Math::Optimization::CLineSearchBasedMethod::~CLineSearchBasedMethod ()
{
    if (_ppLineSearchBasedMethod != NULL)
    {
	    delete _ppLineSearchBasedMethod;
        _ppLineSearchBasedMethod = NULL;
    }
}
Cephei::QL::Math::Optimization::CLineSearchBasedMethod::!CLineSearchBasedMethod ()
{
    if (_ppLineSearchBasedMethod != NULL)
    {
	    delete _ppLineSearchBasedMethod;
    }
}
QuantLib::LineSearchBasedMethod& Cephei::QL::Math::Optimization::CLineSearchBasedMethod::GetReference ()
{
    if (_ppLineSearchBasedMethod == NULL) throw gcnew NativeNullException ();
	return **_ppLineSearchBasedMethod;
}
boost::shared_ptr<QuantLib::LineSearchBasedMethod>& Cephei::QL::Math::Optimization::CLineSearchBasedMethod::GetShared ()
{
    if (_ppLineSearchBasedMethod == NULL) throw gcnew NativeNullException ();
	return *_ppLineSearchBasedMethod;
}
QuantLib::LineSearchBasedMethod* Cephei::QL::Math::Optimization::CLineSearchBasedMethod::GetPointer ()
{
    if (_ppLineSearchBasedMethod == NULL) throw gcnew NativeNullException ();
	return &**_ppLineSearchBasedMethod;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LineSearchBasedMethod>& Cephei::QL::Math::Optimization::CLineSearchBasedMethod::GetHandle ()
{
	if (_phLineSearchBasedMethod == NULL)
	{
		_phLineSearchBasedMethod = new Handle<QuantLib::LineSearchBasedMethod> (*_ppLineSearchBasedMethod);
	}
	return *_phLineSearchBasedMethod;
}
#endif
bool Cephei::QL::Math::Optimization::CLineSearchBasedMethod::HasNative () 
{
	return (_ppLineSearchBasedMethod != NULL);
}

QL::Math::Optimization::EndCriteria::TypeEnum Cephei::QL::Math::Optimization::CLineSearchBasedMethod::Minimize (Cephei::QL::Math::Optimization::IProblem^ P, Cephei::QL::Math::Optimization::IEndCriteria^ endCriteria)
{
    CProblem^ _CP;
    CEndCriteria^ _CendCriteria;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CP = safe_cast<CProblem^> (P);
        _CP->Lock();
        QuantLib::Problem& _P = static_cast<QuantLib::Problem&> (_CP->GetReference ()); 
        _CendCriteria = safe_cast<CEndCriteria^> (endCriteria);
        _CendCriteria->Lock();
        QuantLib::EndCriteria& _endCriteria = static_cast<QuantLib::EndCriteria&> (_CendCriteria->GetReference ()); 
    	QuantLib::EndCriteria::Type _rv = (QuantLib::EndCriteria::Type)(*_ppLineSearchBasedMethod)->minimize ( _P,  _endCriteria );   
        QL::Math::Optimization::EndCriteria::TypeEnum _nrv = (QL::Math::Optimization::EndCriteria::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CP != nullptr) _CP->Unlock();
        if (_CendCriteria != nullptr) _CendCriteria->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

